package com.xiaobai.SynchrobizedLock;

import com.xiaobai.util.ObjectHeadUtil;
import com.xiaobai.util.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

/*
 * biased锁
 */
@Slf4j
public class BiasedLocks {
    //-XX:BiasedLockingStartupDelay=0
//    public static void main(String[] args) {
//        Dog dog = new Dog();
//        log.debug(ClassLayout.parseInstance(dog).toPrintable()); //00000000 00000000 00000000 00000000 00000001 00000000 00000000 00000000
//        synchronized (dog){
//            log.debug(ClassLayout.parseInstance(dog).toPrintable()); //01001010 00000000 00000000 00000000 00010000 11110001 00111111 00111110
//        }
//        //偏向锁默认是延时加载的
//        log.debug(ClassLayout.parseInstance(dog).toPrintable()); //00000000 00000000 00000000 00000000 00000001 00000000 00000000 00000000
//    }
    public static void main(String[] args) throws InterruptedException {
        // 等待偏向锁启动
        Thread.sleep(5000);
        /*
        |-------------------------------------------------------|--------------------|
        | Mark Word (32 bits)                                   | State              |
        |-------------------------------------------------------|--------------------|
        | hashcode:25 | age:4 | biased_lock:0 | 01              | Normal             |
        |-------------------------------------------------------|--------------------|
        | thread:23 | epoch:2 | age:4 | biased_lock:1 | 01      | Biased             |
        |-------------------------------------------------------|--------------------|
        | ptr_to_lock_record:30 | 00                            | Lightweight Locked |
        |-------------------------------------------------------|--------------------|
        | ptr_to_heavyweight_monitor:30 | 10                    | Heavyweight Locked |
        |-------------------------------------------------------|--------------------|
        |                                 | 11                  | Marked for GC      |
        |-------------------------------------------------------|--------------------|
         */
        Dog dog = new Dog();
        log.debug(ClassLayout.parseInstance(dog).toPrintable()); // 可偏向状态 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000101
        synchronized (dog) {
            log.debug(ClassLayout.parseInstance(dog).toPrintable()); // 偏向锁状态 00000000 00000000 00000001 11101101 01011101 11100010 11111000 00000101
        }
        log.debug(ClassLayout.parseInstance(dog).toPrintable()); // 偏向锁状态 00000000 00000000 00000001 11101101 01011101 11100010 11111000 00000101
    }

}
class Dog{

}
